<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\FirstupService;
use App\Services\SdkService;
use Illuminate\Http\Request;

class FirstupController extends Controller
{
    private function getLaunchInfo($info)
    {
        $data = [
            'package_id'    => 0,
            'ad_package_id' => '',
            'click_id'      => '',
            'clue_token'    => '',
        ];

        if (!$info) {
            return $data;
        }

        $info = json_decode($info, true);
        if (isset($info['query'])) {
            // 包id/计划id
            $package_id = $info['query']['package_id'] ?? 0;
            $data['package_id'] = intval($package_id);

            // 媒体自行分包标签
            $data['ad_package_id'] = $info['query']['ad_package_id'] ?? '';

            // 头条广告启动附带路径参数clue_token,用于api回传
            if (isset($info['query']['clue_token'])) {
                $data['clue_token'] = $info['query']['clue_token'] ?? '';
            }

            // 每个媒体附带的路径参数映射到click_id
            $maps = [
                'gdt_vid'   => 'gdt_vid',     // 广点通
                'req_id'    => 'req_id',      // 头条路径参数有 req_id 和 clue_token
                'track_id'  => 'track_id',    // Bilibili
                'ksChannel' => 'callback',    // 快手
                'bd_vid'    => 'bd_vid',      // 百度
                'uctrackid' => 'uctrackid',   // UC
            ];
            foreach ($maps as $key => $val) {
                if (isset($info['query'][$key])) {
                    // 如果出现已经赋值过click_id，证明存在参数冲突，此处进行打印方便后续排查
                    if (!empty($data['click_id'])) {
                        yqlog($data);
                    }
                    if (in_array($key, ['ksChannel'])) {
                        $data['click_id'] = md5($info['query'][$val] ?? '');
                    } else {
                        $data['click_id'] = $info['query'][$val] ?? '';
                    }
                }
            }
        }

        return $data;
    }

    public function firstup(Request $request)
    {
        $params = $request->all();

        // 校验签名
        $ret = SdkService::getInstance()->checkSign($params);
        if ($ret === false) {
            return yqerr(101);
        }

        $package_id    = $params['package_id'] ?? 0;
        $ad_package_id = $params['ad_package_id'] ?? '';
        $click_id      = $params['click_id'] ?? '';
        $clue_token    = $params['clue_token'] ?? '';

        // 如果是小游戏上报，提取clickid等数据
        // 小游戏有冷启动 launch_info 和 热启动 onshow_info 两个数据集
        // 此处优先使用 onshow 数据，没有则使用 launch 数据
        $onshow_info = $this->getLaunchInfo($request->input('onshow_info'));
        $launch_info = $this->getLaunchInfo($request->input('launch_info'));

        foreach (['package_id', 'ad_package_id', 'click_id', 'clue_token'] as $key) {
            if (!empty($onshow_info[$key])) {
                $$key = $onshow_info[$key];
            } elseif (!empty($launch_info[$key])) {
                $$key = $launch_info[$key];
            }
        }

        $params['ad_package_id'] = $ad_package_id;
        $params['package_id']    = $package_id;
        $params['click_id']      = $click_id;
        $params['clue_token']    = $clue_token;

        FirstupService::getInstance()->firstup($params);

        return yqok();
    }
}
